using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VOL.Entity.DomainModels;
using SqlSugar;

namespace VOL.demo.Repositories
{
    public partial class PurchaseOrderItemModelRepository
    {
        /// <summary>
        /// 根据进货单号获取明细列表
        /// </summary>
        /// <param name="orderCode">进货单号</param>
        /// <returns>明细列表</returns>
        public async Task<List<PurchaseOrderItemModel>> GetItemsByOrderCodeAsync(string orderCode)
        {
            return await DbContext.Queryable<PurchaseOrderItemModel>()
                .Where(x => x.OrderCode == orderCode && x.IsDel == 0)
                .ToListAsync();
        }

        /// <summary>
        /// 根据进货单号获取明细列表（连表查询单位名称和当前库存，返回DTO）
        /// </summary>
        /// <param name="orderCode">进货单号</param>
        /// <returns>包含关联数据的DTO列表</returns>
        public async Task<List<PurchaseOrderItemDto>> GetItemsWithJoinByOrderCodeAsync(string orderCode)
        {
            return await DbContext.Queryable<PurchaseOrderItemModel>()
                // 连接单位表，获取单位名称（只查询未删除的单位）
                .LeftJoin<unitmodel>((poi, u) => poi.UnitId == u.Id && u.IsDelete == 0)
                // 连接货品信息表，获取实时库存（只查询未删除的货品）
                .LeftJoin<productinformationmodel>((poi, u, p) => poi.GoodsId == p.Id && p.IsDel == 0)
                .Where((poi, u, p) => poi.OrderCode == orderCode && poi.IsDel == 0)
                .Select((poi, u, p) => new PurchaseOrderItemDto
                {
                    Id = poi.Id,
                    OrderCode = poi.OrderCode,
                    GoodsId = poi.GoodsId,
                    GoodsCode = poi.GoodsCode,
                    GoodsName = poi.GoodsName,
                    SpecModel = poi.SpecModel,
                    UnitId = poi.UnitId,
                    UnitName = u.UnitName,  // 从单位表获取单位名称
                    ProductTypeId = poi.ProductTypeId,
                    CurrentInventory = poi.CurrentInventory,  // 进货时的历史库存
                    RealtimeInventory = p.currentInventory,  // 从货品信息表获取实时库存
                    PurchaseQty = poi.PurchaseQty,
                    PurchasePrice = poi.PurchasePrice,
                    Amount = poi.Amount,
                    ItemRemarks = poi.ItemRemarks,
                    IsDel = poi.IsDel,
                    Creator = poi.Creator,
                    CreateDate = poi.CreateDate,
                    Modifier = poi.Modifier,
                    ModifyDate = poi.ModifyDate,
                    CreateID = poi.CreateID,
                    ModifyID = poi.ModifyID
                })
                .ToListAsync();
        }

        /// <summary>
        /// 批量添加进货明细
        /// </summary>
        /// <param name="items">明细列表</param>
        /// <returns>是否成功</returns>
        public async Task<bool> AddItemsAsync(List<PurchaseOrderItemModel> items)
        {
            try
            {
                return await DbContext.Insertable(items).ExecuteCommandAsync() > 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 根据进货单号删除明细
        /// </summary>
        /// <param name="orderCode">进货单号</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteItemsByOrderCodeAsync(string orderCode)
        {
            try
            {
                return await DbContext.Updateable<PurchaseOrderItemModel>()
                    .SetColumns(x => new PurchaseOrderItemModel { IsDel = 1 })
                    .Where(x => x.OrderCode == orderCode)
                    .ExecuteCommandAsync() > 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 更新明细项
        /// </summary>
        /// <param name="item">明细项</param>
        /// <returns>是否成功</returns>
        public async Task<bool> UpdateItemAsync(PurchaseOrderItemModel item)
        {
            try
            {
                return await DbContext.Updateable(item)
                    .Where(x => x.Id == item.Id)
                    .ExecuteCommandAsync() > 0;
            }
            catch
            {
                return false;
            }
        }
    }
}
